package learn.advance.map1;

import com.sun.org.apache.xpath.internal.axes.HasPositionalPredChecker;
import org.omg.CORBA.INTERNAL;

import javax.xml.transform.Source;
import java.util.*;

class Demo1Map{
    public static void Test(){
        Show1();
    }

    private static void Show1(){
        Map<String,Integer> map=new HashMap<>();
        map.put("小明",18);
        map.put("小军",20);
        map.put("小红",25);
        boolean b1=map.containsKey("小明");
        System.out.println(b1);

        Integer v1=map.get("小明");
        System.out.println(v1);

        Integer v2=map.remove("小军");
        System.out.println(v2);

        System.out.println(map);

        Set<String> set=map.keySet();
        Iterator<String> it=set.iterator();

        System.out.println("=====================");
        while (it.hasNext()){
            String key=it.next();
            Integer v=map.get(key);
            System.out.println(key+":"+v);
        }

        System.out.println("=====================");

        for(String s:set){
            Integer v=map.get(s);
            System.out.println(s+":"+v);
        }

        System.out.println("=====================");

        for(String key:map.keySet()){
            Integer v=map.get(key);
            System.out.println(key+":"+v);
        }
    }
}

class Dmeo3EntrySet{
    public static void Test(){
        Map<String,Integer> map=new HashMap<>();
        map.put("小明",18);
        map.put("小军",20);
        map.put("小红",25);

        Set<Map.Entry<String,Integer>> set=map.entrySet();

        Iterator<Map.Entry<String,Integer>> it=set.iterator();

        while (it.hasNext()){
            Map.Entry<String,Integer> entry=it.next();
            String key=entry.getKey();
            Integer v=entry.getValue();
            System.out.println(key+":"+v);
        }

        System.out.println("==============");
        for(Map.Entry<String,Integer> entry:set){
            String key=entry.getKey();
            Integer v=entry.getValue();
            System.out.println(key+":"+v);
        }
    }
}


class Person{
    private String name_;
    private int age_;

    public Person(){

    }

    public Person(String name,int age){
        this.age_=age;
        this.name_=name;
    }

    @Override
    public String toString(){
        return  "Person{" +
                "name='" + name_ + '\'' +
                ", age=" + age_ +
                '}';
    }

    @Override
    public boolean equals(Object o){
        if(this==o) return true;

        if(o==null||getClass()!=o.getClass())
            return false;

        Person person=(Person)o;
        return age_==person.age_&&Objects.equals(name_,person.name_);

    }

    @Override
    public int hashCode(){
        return Objects.hash(name_,age_);
    }
}


class Demo4HashSavePerson{
    public static void Test(){

    }

    private static void Show1(){
        Map<Person,String> map=new HashMap<>();

        map.put(new Person("小明",18),"中国");
        map.put(new Person("小军",20),"英国");

        Set<Map.Entry<Person,String>> set=map.entrySet();

        for(Map.Entry<Person,String> entry:set){
            Person key=entry.getKey();
            String v=entry.getValue();
            System.out.println(key+":"+v);
        }
    }
}

class Demo1LinkedHashMap{

    public static void Test(){
        HashMap<String,String> map=new HashMap<>();
        map.put("c","c");
        map.put("a","a1");
        map.put("b","b1");
        System.out.println(map);

        LinkedHashMap<String,String> linked=new LinkedHashMap<>();
        linked.put("c","c1");
        linked.put("a","a1");
        linked.put("b","b1");
        System.out.println(linked);
    }
}

class Demo2Hashtable{
    public static void Test(){
        HashMap<String,String> map=new HashMap<>();
        map.put(null,"a");
        map.put("b",null);
        map.put(null,null);
        System.out.println(map);

        Hashtable<String,String> table=new Hashtable<>();
        //table.put(null,null);
    }
}


public class TestMap {
    public static void main(String[] args) {
       // Demo1Map.Test();
       // Dmeo3EntrySet.Test();
        //Demo1LinkedHashMap.Test();
        Demo2Hashtable.Test();
    }
}
